package com.uls.zsw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import com.uls.common.utils.DateUtils;
import com.uls.zsw.domain.ElectricityManage;
import com.uls.zsw.mapper.ElectricityManageMapper;
import com.uls.zsw.mapper.MeterInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.zsw.mapper.ElectricityUsageInfoMapper;
import com.uls.zsw.domain.ElectricityUsageInfo;
import com.uls.zsw.service.IElectricityUsageInfoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用电信息Service业务层处理
 * 
 * @author zsw
 * @date 2024-11-25
 */
@Service
public class ElectricityUsageInfoServiceImpl implements IElectricityUsageInfoService 
{

    @Autowired
    private ElectricityUsageInfoMapper electricityUsageInfoMapper;

    @Autowired
    private MeterInfoMapper meterInfoMapper;

    @Autowired
    private ElectricityManageMapper electricityManageMapper;

    /**
     * 查询用电信息
     * 
     * @param electricityUsageId 用电信息主键
     * @return 用电信息
     */
    @Transactional
    @Override
    public ElectricityUsageInfo selectElectricityUsageInfoByElectricityUsageId(Long electricityUsageId)
    {
        ElectricityUsageInfo electricityUsageInfo=electricityUsageInfoMapper.selectElectricityUsageInfoByElectricityUsageId(electricityUsageId);
        String mm=meterInfoMapper.getMMByMI(electricityUsageInfo.getMeterId());
        String dn=electricityManageMapper.getDNByDI(electricityUsageInfo.getDormitoryId());
        electricityUsageInfo.setSelectedmm(mm);
        electricityUsageInfo.setSelecteddn(dn);
        return electricityUsageInfo;
    }

    /**
     * 查询用电信息列表
     * 
     * @param electricityUsageInfo 用电信息
     * @return 用电信息
     */
    @Transactional
    @Override
    public List<ElectricityUsageInfo> selectElectricityUsageInfoList(ElectricityUsageInfo electricityUsageInfo)
    {
        List<ElectricityUsageInfo> electricityUsageInfoList=electricityUsageInfoMapper.selectElectricityUsageInfoList(electricityUsageInfo);
        if(electricityUsageInfoList!=null){
            for(ElectricityUsageInfo electricityUsageInfo1:electricityUsageInfoList){
                String mm=meterInfoMapper.getMMByMI(electricityUsageInfo1.getMeterId());
                String dn=electricityManageMapper.getDNByDI(electricityUsageInfo1.getDormitoryId());
                ElectricityManage electricityManage=electricityManageMapper.selectByDormitoryId(electricityUsageInfo1.getDormitoryId());
                electricityUsageInfo1.setElectricstatus(electricityManage.getElectricityStatus());
                electricityUsageInfo1.setSelectedmm(mm);
                electricityUsageInfo1.setSelecteddn(dn);
            }
        }
        return electricityUsageInfoList;
    }

    /**
     * 新增用电信息
     * 
     * @param electricityUsageInfo 用电信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertElectricityUsageInfo(ElectricityUsageInfo electricityUsageInfo)
    {
        electricityUsageInfo.setCreateTime(DateUtils.getNowDate());
        Long MI=meterInfoMapper.getMIByMM(electricityUsageInfo.getSelectedmm());
        Long DI=electricityManageMapper.getDIByDN(electricityUsageInfo.getSelecteddn());
        electricityUsageInfo.setMeterId(MI);
        electricityUsageInfo.setDormitoryId(DI);
        electricityUsageInfo.setEndDate(0);
        int result=electricityUsageInfoMapper.insertElectricityUsageInfo(electricityUsageInfo);
        return result;
    }

    /**
     * 修改用电信息
     * 
     * @param electricityUsageInfo 用电信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateElectricityUsageInfo(ElectricityUsageInfo electricityUsageInfo)
    {
        electricityUsageInfo.setUpdateTime(DateUtils.getNowDate());
        Long MI=meterInfoMapper.getMIByMM(electricityUsageInfo.getSelectedmm());
        Long DI=electricityManageMapper.getDIByDN(electricityUsageInfo.getSelecteddn());
        electricityUsageInfo.setMeterId(MI);
        electricityUsageInfo.setDormitoryId(DI);
        return electricityUsageInfoMapper.updateElectricityUsageInfo(electricityUsageInfo);
    }

    /**
     * 批量删除用电信息
     * 
     * @param electricityUsageIds 需要删除的用电信息主键
     * @return 结果
     */
    @Override
    public int deleteElectricityUsageInfoByElectricityUsageIds(Long[] electricityUsageIds)
    {
        return electricityUsageInfoMapper.deleteElectricityUsageInfoByElectricityUsageIds(electricityUsageIds);
    }

    /**
     * 删除用电信息信息
     * 
     * @param electricityUsageId 用电信息主键
     * @return 结果
     */
    @Override
    public int deleteElectricityUsageInfoByElectricityUsageId(Long electricityUsageId)
    {
        return electricityUsageInfoMapper.deleteElectricityUsageInfoByElectricityUsageId(electricityUsageId);
    }

    @Override
    public ElectricityUsageInfo selectElectricityUsageInfoByDormitoryId(Long dormitoryId) {
        return electricityUsageInfoMapper.selectElectricityUsageInfoByDormitoryId(dormitoryId);
    }

}
